package com.koron.ticket.workchange.ticketchangemember.controller;


import cn.hutool.core.util.ObjectUtil;
import com.koron.bean.base.Response;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMember;
import com.koron.ticket.workchange.ticketchangemember.mapper.ChanageMemberDetMapper;
import com.koron.ticket.workchange.ticketchangemember.mapper.TicketChangeMemberMapper;
import com.koron.ticket.workchange.ticketchangemember.service.TicketChangeMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * (TicketChangeMember)表控制层
 *
 * @author 朱超
 * @since 2021-10-27 10:19:48
 */
@Api(tags = "工作班组成员变更接口")
@RestController
@RequestMapping("ticketChangeMember")
public class TicketChangeMemberController {

    @Autowired
    private TicketChangeMemberService ticketChangeMemberService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 新增单条数据
     *
     * @param
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增单条数据")
    public Response insert(@RequestBody TicketChangeMember ticketChangeMember) {
        return ADOConnection.runTask(getCurrDs(), ticketChangeMemberService, "insert", Response.class, ticketChangeMember);
    }

    /**
     * 根据id删除单条数据
     *
     * @param id 主键
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @DeleteMapping("deleteById")
    @ApiOperation(value = "根据id删除单条数据")
    public Response deleteById(@RequestParam("id") String id) {
        return ADOConnection.runTask(getCurrDs(), ticketChangeMemberService, "deleteById", Response.class, id);
    }

    /**
     * 修改数据
     *
     * @param ticketChangeMember
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PutMapping("update")
    @ApiOperation(value = "修改单条配置详情")
    public Response update(@RequestBody TicketChangeMember ticketChangeMember) {
        return ADOConnection.runTask(getCurrDs(), ticketChangeMemberService, "update", Response.class, ticketChangeMember);
    }

    /**
     * 通过id查询单条数据
     *
     * @param id 主键
     * @return Response
     * @see Response
     * @version 1.0
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("getById")
    public Response getById(@RequestParam("id") String id) {
        return ADOConnection.runTask(getCurrDs(), ticketChangeMemberService, "getById", Response.class, id);
    }

    /**
     *  最新变更
     *
     * @param ticketId 主键
     * @return Response
     * @see Response
     * @version 1.0
     */
    @ApiOperation(value = "根据ticketId查询 查最新")
    @GetMapping("getByTicket")
    public Response getByTicket(@RequestParam("ticketId") String ticketId) {
//        return ADOConnection.runTask(getCurrDs(), ticketChangeMemberService, "getByTicket", Response.class, ticketId);
        TicketChangeMember ticketChangeMember = ADOConnection.runTask(getCurrDs(), factory -> {
            TicketChangeMember byTicket = factory.getMapper(TicketChangeMemberMapper.class).getByTicket(ticketId);
            if(ObjectUtil.isNotNull(byTicket)){
                byTicket.setDet(factory.getMapper(ChanageMemberDetMapper.class).queryByChanageId(byTicket.getId()));
            }
            return byTicket;
        }, TicketChangeMember.class);
        return Response.success(ticketChangeMember);
    }

}
